/*	$Id: userdb.c 102 2007-01-09 06:40:05Z phrakt $	*/
/*
 * Copyright (c) 2006 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "user.h"
#include "private.h"


struct dom_usrdb_hdr {
	uint32_t     dbh_magic;
	uint32_t     dbh_version;
	uint32_t     dbh_nbent;
};


struct dom_usrdb_entry {
};


#define DOM_USRDB_DIRTY          0x01

struct dom_usrdb {
	int                       db_fd;
	int                       db_flags;
	char                     *db_path;
	struct dom_usrdb_hdr      db_hdr;
	struct dom_usrdb_entry  **db_ent;

};


static int   dom_usrdb_load  (dom_usrdb_t *);




/*
 * dom_usrdb_open()
 */
dom_usrdb_t*
dom_usrdb_open(const char *path, int flags, mode_t mode)
{
	int fd;
	ssize_t ret;
	dom_usrdb_t *db;
	struct stat st;
	struct dom_usrdb_hdr hdr;
	struct dom_usrdb_entry *ent;

	if ((fd = open(path, flags, mode)) == -1) {
		return (NULL);
	}

	(void)fstat(fd, &st);

	if ((db = malloc(sizeof(*db))) == NULL) {
		(void)close(fd);
		return (NULL);
	}

	memset(db, 0, sizeof(*db));

	db->db_fd = fd;
	if ((db->db_path = strdup(path)) == NULL) {
		(void)close(fd);
		free(db);
		return (NULL);
	}

	db->db_hdr.dbh_magic = DOM_USERDB_MAGIC;
	db->db_hdr.dbh_version = DOM_USERDB_VERSION;

	if ((st.st_size > 0) && (dom_usrdb_load(db) == -1)) {
		dom_usrdb_close(db);
		return (NULL);
	}

	return (db);
}

/*
 * dom_usrdb_sync()
 *
 */
int
dom_usrdb_sync(dom_usrdb_t *db)
{
	int i;
	ssize_t ret;

	if (!(db->db_flags & DOM_USRDB_DIRTY))
		return (0);

	if (lseek(db->db_fd, 0, SEEK_SET) == -1) {
		return (-1);
	}

	ret = write(db->db_fd, &db->db_hdr, sizeof(db->db_hdr));
	if (ret == -1) {
		return (-1);
	}

	for (i = 0; i < db->db_hdr.dbh_nbent; i++) {
		ret = write(db->db_fd, db->db_ent[i], sizeof(db->db_ent[i]));
	}

}

/*
 * dom_usrdb_close()
 *
 */
int
dom_usrdb_close(dom_usrdb_t *db)
{
	unsigned int i;

	if (db->db_fd != -1) {
		if (db->db_flags & DOM_USRDB_DIRTY)
			dom_usrdb_sync(db);

		(void)close(db->db_fd);
	}

	if (db->db_path != NULL)
		free(db->db_path);

	for (i = 0; i < db->db_hdr.dbh_nbent; i++)
		free(db->db_ent[i]);
	free(db->db_ent);

	free(db);
}

/*
 * dom_usrdb_adduser()
 *
 */
int
dom_usrdb_adduser(dom_usrdb_t *db, dom_user_t *user)
{


}

/*
 * dom_usrdb_load()
 *
 */
static int
dom_usrdb_load(dom_usrdb_t *db)
{
	int i;
	ssize_t ret;
	struct dom_usrdb_entry *ent;

	ret = read(db->db_fd, &(db->db_hdr), sizeof(db->db_hdr));
	if (ret == -1)
		return (-1);

	if (ret != sizeof(db->db_hdr)) {
	}

	for (i = 0; i < db->db_hdr.dbh_nbent; i++) {
		if ((ent = malloc(sizeof(*ent))) == NULL) {
		}
	}

	return (0);
}
